<?php
$document_title = 'FakeBank :: PHP + DB Result page: City Populations';
$document_meta_description = '<meta name="description" content="PHP DB results displayed">';
$breadcrumbs = 'FakeBank PHP DB Result page';
include "fakebank_header.html"; ?>

<!-- Set up the nav buttons how you like for each page. Specifically, the 'selected' class. 
		Leave start and end classes on first and last buttons. -->
	<nav>
    	<ul>
        	<li class="start"><a href="index.php">Home</a></li>
            <li><a href="styling.php">Styling Examples</a></li>
            <li><a href="template.php">Template page</a></li>
            <li><a href="php_binding.php">PHP DB Binding</a></li>
            <li class="end"><a href="#">Contact Us</a></li>
        </ul>
    </nav>

<!-- Breadcrumb trail -->
<div id="breadcrumbs"><a href="index.php">FakeBank</a> <?php if (isset ($breadcrumbs))echo ' > '.$breadcrumbs; ?> </div>


    <div id="body">
        <section id="content">
	    <article>
	    
		<?php
			//First we want to check if we got input and if so is it sane -- do we want to allow it into our DB??
			if(isset($_POST['population-form-input']))
			{
					//POST variables are strings. We want an int. So we want to check to make sure there's only numbers and commas.
					$input = filter_input(INPUT_POST, 'population-form-input', FILTER_VALIDATE_INT);
					//$input is either an int or null now.
			}
			
			
			echo '<h1>Results</h1>';
			
			echo "<p>First we write a pretty header. Note at this point we still haven't actually touched the database. We did on the last page but that was fluff, showing how to connect. Now on this page we will connect again and actually use the connection this time.</p>";
			
			//If we don't have input, meaning it's null b/c it wasn't an int, stop right now. Show them the page and do nothing else w/the input.
			if(!$input)
			{
				echo '<strong>Sorry, that is not valid input. Only input whole numbers.</strong><br>';
			
				echo '</article>';
				echo '</section>';
        
			  	echo '<div class="clear"></div>';
				echo '</div> <!--end body div -->';
		
				include "fakebank_footer.html"; 		
			} 
			
			//If we get here, the input is good.			
			echo '<h2>Cities with > ' .$_POST['population-form-input'] .' people</h2>';
						
			echo "<p>Now we're going to connect to the database again, since we have our user input ready from our POST and form. <strong>Also, you should notice that we use &quot; instead of ' on the echo when we are echo'ing out stuff that contains '.</strong>	</p>";
			
			/*BLANK FOR SECURITY REASONS w/svn.
			However, the values you put here should look like this. Let me know if you have problems.
			Note we have one single quote at the start and end; this 1st one is all 1 string:
			$dbname = 'mysql:host=this-is-probably-localhost;dbname=put-name-of-db-to-use-here';
			$user = 'username-for-db-access';	
			$pass = 'secret-password';*/
			$dbname = '';
			$user = '';	
			$pass = '';		
		
			//TRY to connect AND THEN try to call our stored procedure. We know by now that the input's ok.
			try
			{
				$dbhandle = new PDO($dbname, $user, $pass);
				
				try
				{
					//Prep the procedure. ? means a parameter is missing.
					$procedure = "CALL get_populated_cities(?)";
					
					//Prepare the connection for the procedure.
					$call = $dbhandle->prepare($procedure);
					
					//Identify what the missing parameter is. Just do this n times for n parameters.
					$call->bindParam(1, $input);
					
					//DO IT!!!!
					$results = $call->execute();
					
					//Something has gone wrong?
					if (!$results)
					{
						throw new Exception("Couldn't fetch populated cities");
					}
					
					//Sure we have results, but let's get an array from them.
					$results = $call->fetchAll();

					//Kill the DB connection, we're done w/it now
					$dbhandle = null;
					
					//Uncomment this to see the raw/returned array results on your browser
					//var_dump($results);
				}
				
				catch (Exception $e)
				{
					throw new Exception("An exception occurred while getting population data");
					//throw new Exception("An exception occurred while getting population data: " . $e->getMessage());
				}	
			} //end try from line 65
			
			//Catch errors, but mind what you display on a public webpage concerning them.
			catch(PDOException $e)
			{
				echo "DB Connection error. Error details not exposed on this webpage.";
				
				/*Uncomment this line if you want to see the error. Not a great idea
				for a live webpage but useful while building stuff:
				echo  $e->getMessage();*/
				
				echo "<br>Dying on failed connect. Remainder of page will fail to render.";
				die();
			}
			
			echo 'We will now do something with the results. There are ' .sizeof($results) .' results!<br>';
			
			echo "<p>Everyone likes tables. Let's make a table with the results.</p>";
			echo "<p><span STYLE='color:red'>OK, I lied.</span> I'm off to do errands now. I'll try to get the table output up before our class, though. In the meantime, did you see how you can get at the output above? It's a commented line. Search for var_dump, uncomment it, and load the page again.</p>";
			
		//Close php code	
		?>
		
		</article>
        </section>
        
        
    	<div class="clear"></div>
		</div> <!--end body div -->
		
<?php include "fakebank_footer.html"; ?>		